﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Data;
using System.Globalization;
using EntLibContrib.Query.Commands;
using EntLibContrib.Query.QuickStart.CustomCommands.WcfDataService;

namespace EntLibContrib.Query.QuickStart.CustomCommands
{
    using Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;

    /// <summary>
	/// Custom service command to convert a Basic2 return value to a DataSet
	/// </summary>
    [ConfigurationElementType(typeof(ReadBasic2ObjectByIdCommandData))]
    public class ReadBasic2ObjectByIdCommand : ServiceCommand
	{
		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="ReadBasic2ObjectByIdCommand"/> class.
		/// </summary>
		public ReadBasic2ObjectByIdCommand()
		{
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Executes a read action.
		/// </summary>
		/// <returns>
		/// a DataSet containing the results of the read action
		/// </returns>
		public override DataSet ExecuteForRead()
		{
			WcfDataServiceClient serviceClient = new WcfDataServiceClient(Endpoint);

			return ConvertBasic2ToDataSet(serviceClient.ReadBasic2ObjectById((Guid)Parameters["id"].Value));
		}

		/// <summary>
		/// Executes a write action.
		/// </summary>
		public override void ExecuteForWrite()
		{
			WcfDataServiceClient serviceClient = new WcfDataServiceClient(Endpoint);

			serviceClient.UpdateBasic2((Guid)Parameters["id"].Value,
																(string)Parameters["name"].Value,
																(string)Parameters["description"].Value,
																(Guid)Parameters["ownerId"].Value,
																(Guid)Parameters["updatedIdentityId"].Value,
																(DateTime)Parameters["updatedDate"].Value
																);
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Converts the basic2 to data set.
		/// </summary>
		/// <param name="basic2Object">The basic2 object.</param>
		/// <returns>A populated dataset containing a single row</returns>
		private static DataSet ConvertBasic2ToDataSet(Basic2 basic2Object)
		{
			DataSet basic2DataSet = new DataSet("Basic2Collection");
			basic2DataSet.Locale = CultureInfo.CurrentCulture;
			basic2DataSet.Tables.Add("Basic2");
			basic2DataSet.Tables[0].Columns.Add("Id", typeof(Guid));
			basic2DataSet.Tables[0].Columns.Add("Name", typeof(string));
			basic2DataSet.Tables[0].Columns.Add("Description", typeof(string));
			basic2DataSet.Tables[0].Columns.Add("OwnerId", typeof(Guid));
			basic2DataSet.Tables[0].Columns.Add("CreatedIdentityId", typeof(Guid));
			basic2DataSet.Tables[0].Columns.Add("CreatedDate", typeof(DateTime));
			basic2DataSet.Tables[0].Columns.Add("UpdatedIdentityId", typeof(Guid));
			basic2DataSet.Tables[0].Columns.Add("UpdatedDate", typeof(DateTime));
			basic2DataSet.Tables[0].Columns.Add("DeletedIdentityId", typeof(Guid));
			basic2DataSet.Tables[0].Columns.Add("DeletedDate", typeof(DateTime));
			basic2DataSet.Tables[0].Rows.Add(new object[] { basic2Object.Id,
																											basic2Object.Name,
																											basic2Object.Description,
																											basic2Object.OwnerId,
																											basic2Object.CreatedIdentityId,
																											basic2Object.CreatedDate,
																											basic2Object.UpdatedIdentityId,
																											basic2Object.UpdatedDate,
																											basic2Object.DeletedIdentityId,
																											basic2Object.DeletedDate });
			return basic2DataSet;
		}
		#endregion
	}

    public class ReadBasic2ObjectByIdCommandData : CommandData
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EntLibContrib.Query.QuickStart.CustomCommands.ReadBasic2ObjectByIdCommandData"/> class.
        /// </summary>
        public ReadBasic2ObjectByIdCommandData()
            : base(typeof(ReadBasic1ObjectSortCommand))
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EntLibContrib.Query.QuickStart.CustomCommands.ReadBasic2ObjectByIdCommandData"/> class.
        /// </summary>
        public ReadBasic2ObjectByIdCommandData(string name)
            : base(name, typeof(ReadBasic1ObjectSortCommand))
        {
        }

        public override Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration GetRegistration()
        {
            return
                new TypeRegistration<ICommand>(
                    () => new ReadBasic1ObjectSortCommand())
                {
                    Lifetime = TypeRegistrationLifetime.Transient,
                    Name = Name,
                    IsPublicName = true
                };
        }
    }
}
